package com.xingfudazi.web.controller.app;

import com.xingfudazi.app.domain.AppTrade;
import com.xingfudazi.app.dto.SysAdjustEnergyDTO;
import com.xingfudazi.app.dto.TransferDTO;
import com.xingfudazi.app.service.IAppTradeService;
import com.xingfudazi.app.util.LockUtil;
import com.xingfudazi.common.core.controller.BaseController;
import com.xingfudazi.common.core.domain.Result;
import com.xingfudazi.common.core.page.TableDataInfo;
import com.xingfudazi.common.utils.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 交易表 前端控制器
 * </p>
 *
 * @author Yu JiaMing
 * @since 2023-11-13
 */
@Api(tags = {"交易表"})
@RestController
@RequestMapping("/trade")
public class AppTradeController extends BaseController {

    private Logger log = LoggerFactory.getLogger(getClass());

    @Resource
    private IAppTradeService appTradeService;

    @ApiOperation(value = "交易号查询交易详情")
    @GetMapping("{id}")
    public Result<AppTrade> findById(@PathVariable Long id) {
        return Result.success(appTradeService.findById(id));
    }


    @ApiOperation(value = "订单号查询交易详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderNo", value = "订单号", dataTypeClass = String.class),
    })
    @GetMapping("order")
    public Result<List<AppTrade>> findByOrderNo(@RequestParam String orderNo) {
        return Result.success(appTradeService.findByOrderNo(orderNo));
    }

    /**
     * 系统调整
     * 入参为调整后的
     *
     * @param sysAdjustEnergyDTO 产能调整信息
     * @return Result
     */
    @ApiOperation(value = "系统调整")
    @PostMapping("sys/adjust/Energy")
    public Result<?> sysAdjustEnergy(@RequestBody @Validated SysAdjustEnergyDTO sysAdjustEnergyDTO) {
        appTradeService.sysAdjustEnergy(sysAdjustEnergyDTO);
        return Result.success();
    }


    @ApiOperation(value = "查询个人交易记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "pageCount", value = "每页条数", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "currentTime", value = "当前查询时间）", dataTypeClass = Date.class)
    })
    @GetMapping("history/p")
    public TableDataInfo<AppTrade> historyPerson(Integer page, Integer pageCount, Date currentTime) {
        startPage();
        AppTrade appTrade = new AppTrade();
        appTrade.setWalletId(SecurityUtils.getUserId().toString());
        return getDataTable(this.appTradeService.list(appTrade, currentTime));
    }


    @ApiOperation(value = "pc-查询交易表分页数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "pageCount", value = "每页条数", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "orderNo", value = "订单号", dataTypeClass = String.class),
            @ApiImplicitParam(name = "wallieId", value = "钱包id", dataTypeClass = String.class)
    })
    @GetMapping()
    public TableDataInfo<AppTrade> findListByPage(Integer page, Integer pageCount,String wallieId,String orderNo) {
        startPage();
        AppTrade appTrade = new AppTrade();
        appTrade.setWalletId(wallieId);
        appTrade.setOrderNo(orderNo);
        return getDataTable(this.appTradeService.list(appTrade, null));
    }

    /**
     * 转账
     *
     * @param transferDTO 产能调整信息
     * @return Result
     */
    @ApiOperation(value = "转账")
    @PostMapping("transfer/energy")
    public Result<?> transferEnergy(@RequestBody @Validated TransferDTO transferDTO) {
        Long userId = SecurityUtils.getUserId();
        String key = "transfer" + userId;
        // 接口幂等 ，10s、防止多次提交
        LockUtil.ideLock(key, 1000 * 10);
        try {
            appTradeService.transferEnergy(transferDTO);
        } finally {
            LockUtil.ideUnLock(key);
        }
        return Result.success();
    }


}
